<?php
 
namespace app\common\job;
 
use Yii;
use yii\base\BaseObject;
use app\helpers\DatetimeHelper;
use yii\helpers\ArrayHelper;
use app\modules\notify\service\NotifyService;
use app\modules\notify\service\NotifyRangeService;
use app\modules\notify\service\NotifyLatestService;
use app\modules\notify\service\NotifyRangeLatestService;
use app\modules\email\service\EmailService;
use app\modules\wxofficial\service\WxOfficialService;
use \yii\queue\Job;
 
class NotifySpecialAddJob extends BaseObject implements Job
{
    public $rangeList;
    public $notifyId;
    public $createTimestamp;
    public $isTemporary;
    public $publisherUserId;

    public function execute($queue){
        $rangeList = $this->rangeList;
        $notifyId = $this->notifyId;
        $createTimestamp = $this->createTimestamp;
        $isTemporary = $this->isTemporary;
		$publisherUserId = $this->publisherUserId;

        //添加通知范围
        $range = [];
        $userList = [];
        foreach($rangeList as $key => $temp) {
            $range[$key]['notify_id'] = $notifyId;
            $range[$key]['userid'] = $temp['user_id'];
            $range[$key]['content'] = $temp['content'];
            $range[$key]['send_status'] = $temp['send_status'];
            $range[$key]['create_timestamp'] = $createTimestamp;
            $range[$key]['update_timestamp'] = $createTimestamp;
            $range[$key]['status'] = 1;
            $userList[$key] = $temp['user_id'];
        }
        $rangeIds = (new NotifyRangeService)->addMany($range);
        
        if($isTemporary == 0) {
            // 添加到latest
            (new NotifyRangeLatestService)->addMany($range);
            // (new EmailService)->notifyUnreadEmail($notifyId, $userList);
        }
        //完成所有工作，修改进度
        (new NotifyService)->update(['process'=>2],['id' => $notifyId]);    //阅读中
        if($isTemporary == 0) { // 修改latest
            (new NotifyLatestService)->update(['process'=>2],['id' => $notifyId]);
        }
		
		if($isTemporary == 0) {
			(new WxOfficialService)->notifyUnread($notifyId, $userList, $publisherUserId);
		}
    }
}